﻿using SUNRPA.CONTROLS.IMPL.domain.flowRunningVar;
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace RPAVIEW.views.controlLibrary {
    /// <summary>
    /// VarTreeWindow.xaml 的交互逻辑
    /// </summary>
    public partial class VarTreeWindow:UserControl {
        private static Regex SUBFIELD_PATTERN = new Regex(@"[0-9]+");
        private List<VarTreeNode> originalTree;

        public List<VarTreeNode> OriginalTree { get => originalTree; set => originalTree = value; }

        public VarTreeWindow() {
            InitializeComponent();
        }

        private void SelectVar(object sender, MouseButtonEventArgs e) {
            VarTreeNode node = (sender as TextBlock).DataContext as VarTreeNode;
            string varNameExp = node.Key;
            if(!string.IsNullOrEmpty(node.Expression)) {
                string varExtenExp = node.Expression;
                MatchCollection matchs = SUBFIELD_PATTERN.Matches(varExtenExp);
                if(matchs.Count > 0) {
                    UIElementCollection childs = ((sender as TextBlock).Parent as StackPanel).Children;
                    List<string> texts = new List<string>();
                    foreach(UIElement child in childs) {
                        TextBox textBox = child as TextBox;
                        if(textBox != null) { 
                            texts.Add(textBox.Text);
                        }
                    }
                    string[] extenParts = SUBFIELD_PATTERN.Split(varExtenExp);
                    for(int i = 0; i < extenParts.Length; i++) {
                        string exten = extenParts[i];
                        varNameExp = varNameExp + exten;
                        if(i < extenParts.Length - 1) { 
                            int index = int.Parse(matchs[i].Value);
                            string custVal = texts[index];
                            varNameExp = varNameExp + custVal;
                        }
                    }
                } else { 
                    varNameExp = varNameExp + varExtenExp;
                }
            } else { 
                if(!node.ReflectMem.MemberType.Equals(MemberTypes.TypeInfo)) {
                    string varNameExten = "." + node.ReflectMem.Name;
                    if(node.ReflectMem.MemberType.Equals(MemberTypes.Method)) { 
                        varNameExten = varNameExten+"()";
                    }
                    varNameExp = varNameExp + varNameExten;
                }
            }
            varNameExp = "{" + varNameExp + "}";
            string originalText = (this.DataContext as SelVarTextBox).varNameBox.Text;
            (this.DataContext as SelVarTextBox).varNameBox.Text = originalText + varNameExp;
            (this.DataContext as SelVarTextBox).varNameBox.GetBindingExpression(TextBox.TextProperty).UpdateSource();
            varTree.Visibility = Visibility.Collapsed;
        }

        //搜索树
        private void SearchTree(object sender, RoutedEventArgs e) {
            string value = this.searchValue.Text;
            List<VarTreeNode> temp = new List<VarTreeNode>();
            if(!string.IsNullOrEmpty(value)) {
                foreach(VarTreeNode node in OriginalTree) {
                    if(node.DisplayName.Contains(value)) { 
                        temp.Add(node);
                    }
                }
                this.treeView.ItemsSource = temp;
            } else { 
                this.treeView.ItemsSource = OriginalTree;
            }
        }

        private void StackPanelIni(object sender, EventArgs e) {
            StackPanel sp = sender as StackPanel;
            TextBlock child = sp.Children[0] as TextBlock;
            if(child.Text.Contains("$")) { 
                string[] texts = child.Text.Split('$');
                for(int i = 0; i < texts.Length; i++) { 
                    TextBlock textBlock = new TextBlock();
                    textBlock.MouseLeftButtonDown += SelectVar;
                    textBlock.Text = texts[i];
                    sp.Children.Add(textBlock);
                    if(i < texts.Length - 1) { 
                        TextBox tb = new TextBox();
                        tb.Width = 35;
                        tb.Height = 20;
                        tb.Margin = new Thickness(0);
                        tb.Padding = new Thickness(0);
                        sp.Children.Add(tb);
                    }
                }
                sp.Children.RemoveAt(0);
            }
        }
    }
}
